package com.wkw.jkyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkw.jkyf.mapper.DruginfoMapper;
import com.wkw.jkyf.mapper.OwinfoMapper;
import com.wkw.jkyf.pojo.Druginfo;
import com.wkw.jkyf.pojo.Owinfo;
import com.wkw.jkyf.service.IOwinfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OwinfoServiceImpl extends ServiceImpl<OwinfoMapper, Owinfo> implements IOwinfoService {

   @Resource
   private OwinfoMapper owinfoMapper;
    @Resource
    private DruginfoMapper druginfoMapper;


    @Override
    public IPage<Owinfo> selectOwinfoPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Owinfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param)){
            queryWrapper.like("dname",param);
        }
        queryWrapper.orderByDesc("id");
        Page<Owinfo> page = new Page<>(pageNum,pageSize);
        return owinfoMapper.selectPage(page,queryWrapper);
    }

    @Override
    @Transactional
    //添加出入库信息
    public int addOwinfo(Owinfo owinfo) {
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",owinfo.getDname())
                .eq("number",owinfo.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        //药品的总数量
        Integer stock = druginfo.getStock();
        System.out.println("OwinFo--"+owinfo);
        QueryWrapper<Owinfo> owinfoWrapper = new QueryWrapper<>();
        owinfoWrapper
                .eq("dnumber",owinfo.getDnumber())
                .orderByDesc("id");
        owinfoWrapper.last("limit 1");
        Owinfo oldOwinfo = owinfoMapper.selectOne(owinfoWrapper);
        System.out.println("OldOwinfo---"+oldOwinfo);
        //历史仓库数量
        int oldWareHouse = 0;
        if (oldOwinfo!=null && oldOwinfo.getWarehouse()!= null){
            oldWareHouse = oldOwinfo.getWarehouse();
        }
        //当前出入库数量
        Integer count = owinfo.getCount();
        if("入库".equals(owinfo.getType())){
            if(count>stock||oldWareHouse+count>stock){
                return -1;
            }
            owinfo.setWarehouse(oldWareHouse+count);
        }else if("出库".equals(owinfo.getType())){
            if(count>oldWareHouse){
                return -1;
            }
            owinfo.setWarehouse(oldWareHouse-count);
        }
        //得到当前用户对象
        Subject subject = SecurityUtils.getSubject();
        String userName = (String)subject.getSession().getAttribute("userName");
        owinfo.setOperator(userName);
        owinfo.setCreatetime(new Date());
        return owinfoMapper.insert(owinfo);
    }

    @Override
    public int editOwinfo(Owinfo owinfo) {
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",owinfo.getDname())
                .eq("number",owinfo.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        //药品数量
         int stock = druginfo.getStock();
        QueryWrapper<Owinfo> owinfoWrapper = new QueryWrapper<>();
        owinfoWrapper
                .eq("id",owinfo.getId());
        Owinfo oldOwInfo = owinfoMapper.selectOne(owinfoWrapper);
        QueryWrapper<Owinfo> lastOwinfoWrapper = new QueryWrapper<>();
        lastOwinfoWrapper
                .eq("dnumber",owinfo.getDnumber());
        List<Owinfo> lastOldOwInfo = owinfoMapper.selectList(lastOwinfoWrapper);
        Owinfo owinfo1 = lastOldOwInfo.get(0);
        //仓库数量
        int warehouse = owinfo1.getWarehouse();

        int oldCount = oldOwInfo.getCount();
        int newCount = owinfo.getCount();
        if("入库".equals(oldOwInfo.getType())){
            if(newCount>stock){
                return -1;
            }
            if(newCount>oldCount){
                owinfo.setWarehouse(warehouse+(newCount-oldCount));
            }else {
                owinfo.setWarehouse(warehouse-(oldCount-newCount));
            }
            owinfo.setWarehouse(warehouse+newCount-oldCount);
        }else if("出库".equals(oldOwInfo.getType())){
            if(newCount>warehouse){
                return -1;
            }

                owinfo.setWarehouse(warehouse-newCount);


        }

        return owinfoMapper.updateById(owinfo);
    }

    @Override
    public Owinfo queryOwinfofoById(Integer id) {
        return owinfoMapper.selectById(id);
    }

    @Override
    public int deleteOwinfoById(Integer id) {
        return owinfoMapper.deleteById(id);
    }
}
